package com.serene.im.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serene.im.enums.ImChatVersion;
import com.serene.im.enums.ImDataSources;
import com.serene.im.enums.ImMsgReadStatus;
import com.serene.im.enums.ImMsgType;
import com.serene.im.mapper.ImChatMsgLogsMapper;
import com.serene.im.pojo.ImChatMsgLogs;
import com.serene.im.service.ImChatMsgLogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * im_chat聊天记录 服务实现类
 * </p>
 *
 * @author serene
 * @since 2020-05-26
 */
@Service
public class ImChatMsgLogsServiceImpl extends ServiceImpl<ImChatMsgLogsMapper, ImChatMsgLogs> implements ImChatMsgLogsService {

    @Autowired
    private ImChatMsgLogsMapper imChatMsgLogsMapper;

    /**
     * 添加消息
     *
     * @param imChatMsgLogs 聊天记录类
     * @return
     */
    @Override
    public Integer saveWebMsgLogs(ImChatMsgLogs imChatMsgLogs) {
        LocalDateTime dateTime = LocalDateTime.now();
        imChatMsgLogs.setCreateTime(dateTime);
        imChatMsgLogs.setSendTime(dateTime);
        imChatMsgLogs.setMsgType(ImMsgType.TYPE_1.getCode());
        imChatMsgLogsMapper.insert(imChatMsgLogs);
        return imChatMsgLogs.getId();
    }

    /**
     * 修改是否在线状态为离线
     *
     * @param msgId 签收id
     * @return
     */
    @Override
    public Integer updateOfflineStatusTwo(Integer msgId) {
        ImChatMsgLogs logs = new ImChatMsgLogs();
        logs.setId(msgId);
        logs.setMsgOfflineStatus(2);
        imChatMsgLogsMapper.updateById(logs);
        return msgId;
    }

    /**
     * 消息读取
     *
     * @param msgIdList 签收id List
     */
    @Override
    public void updateMsgReadStatusOne(List<String> msgIdList) {
        System.out.println("进入消息签收service");
        ArrayList<ImChatMsgLogs> batchList = new ArrayList<ImChatMsgLogs>();

        LocalDateTime localDateTime = LocalDateTime.now();
        for (String msg : msgIdList) {
            ImChatMsgLogs logs = new ImChatMsgLogs();
            logs.setId(Integer.valueOf(msg));
            logs.setMsgReadStatus(ImMsgReadStatus.STATUS_1.getCode());
            logs.setReceiveTime(localDateTime);
            batchList.add(logs);
        }
        saveOrUpdateBatch(batchList);
    }


    /**
     * 查询好友聊天记录
     *
     * @param sendId    发送者id
     * @param receiveId 接收者id
     * @return
     */
    @Override
    public List<ImChatMsgLogs> queryFriendLogsList(String sendId, String receiveId) {
        QueryWrapper<ImChatMsgLogs> queryWrapper = new QueryWrapper<ImChatMsgLogs>();
        queryWrapper.eq("to_type", 1);
        //queryWrapper.eq("send_id", sendId).or().eq("receive_id", sendId);
        queryWrapper.and(wrapper -> wrapper.eq("receive_id", receiveId).or().eq("send_id", receiveId));
        queryWrapper.and(wrapper -> wrapper.eq("send_id", sendId).or().eq("receive_id", sendId));
        //queryWrapper.eq("receive_id", receiveId).or().eq("send_id", receiveId);
        return imChatMsgLogsMapper.selectList(queryWrapper);
    }


    /**
     * 根据群id查询群信息记录
     *
     * @param groupInfoId 群id
     * @return
     */
    @Override
    public List<ImChatMsgLogs> queryImChatMsgLogsListByGroupInfoId(Integer groupInfoId) {
        QueryWrapper<ImChatMsgLogs> wrapper = new QueryWrapper<ImChatMsgLogs>();
        wrapper.eq("group_info_id", groupInfoId);
        wrapper.eq("data_status", 1);
        return imChatMsgLogsMapper.selectList(wrapper);
    }


    /**
     * 根据接收者id读取消息
     *
     * @param imChatMsgLogs 聊天记录类
     * @return
     */
    @Override
    public Integer updateImChatMsgListById(ImChatMsgLogs imChatMsgLogs) {
        UpdateWrapper<ImChatMsgLogs> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("send_id", imChatMsgLogs.getSendId());
        updateWrapper.eq("receive_id", imChatMsgLogs.getReceiveId());
        imChatMsgLogs.setMsgReadStatus(1);
        return imChatMsgLogsMapper.update(imChatMsgLogs, updateWrapper);
    }

}
